package top.milkbox.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.milkbox.api.BaiduTranslationApi.BaiduTransApi;
import top.milkbox.info.BaiduTranslationInfo.TranslationSuccessResult;
import top.milkbox.info.requestBody.Translation;
import top.milkbox.service.BaiduTranslationService;

@Service
@Slf4j
public class BaiduTranslationServiceImpl implements BaiduTranslationService {

    // 百度翻译工具
    @Autowired
    private BaiduTransApi transApi;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 将中文翻译为英文
     * 对原文格式进行校验，前后最好不要有空格
     * 长度控制在30个字符以内
     *
     * @param source 原文
     * @return 翻译后的结果对象
     */
    @Override
    public TranslationSuccessResult translate(String source) {
        log.debug("处理字符串：\"" + source + "\"");
        source = source.trim();// 去除前后的空格
        if (source.length() > 30) {
            log.debug("原字符串长度超过30，请减小长度！");
            return null;
        }

        // 发送请求
        TranslationSuccessResult result = null;
        try {
            result = transApi.getTransResult(source, "auto", "en");
        } catch (Exception e) {
            log.error("错误，可能的原因是百度翻译接口返回了报错信息，当前的结果类无法接收，可以考虑用错误信息类进行接收");
        }
        log.debug("百度翻译请求成功，结果集对象：" + result);
        return result;
    }
}
